//
//  GQHCommentController.m
//  Xanthium
//
//  Created by GuanQinghao on 05/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHCommentView.h"
#import "GQHReviewView.h"

#pragma mark Controller
#import "GQHCommentController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHCommentController () {
    
#pragma mark Data
    
#pragma mark View
    /// 自定义根视图
    GQHCommentView *rootView;
}

@end


@implementation GQHCommentController

#pragma mark --Lifecycle
- (void)loadView {
    // comment 提交评论  review 查看评论
    DLog();
    
    rootView = [[GQHCommentView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_orderModel.qh_ownerAvatar];
        NSURL *URL = [NSURL URLWithString:URLString];
        [rootView.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        
        rootView.qh_nameLabel.text = _qh_orderModel.qh_ownerName;
        rootView.qh_subtitleLabel.text = [NSString stringWithFormat:@"评分: %.2f 分",_qh_orderModel.qh_ownerScore];
    } else {
        
        NSString *URLString = [[NSString qh_imageURLString] stringByAppendingPathComponent:_qh_orderModel.qh_driverAvatar];
        NSURL *URL = [NSURL URLWithString:URLString];
        [rootView.qh_avatarImageView sd_setImageWithURL:URL placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        
        rootView.qh_nameLabel.text = _qh_orderModel.qh_driverName;
        rootView.qh_subtitleLabel.text = [NSString stringWithFormat:@"评分: %.2f 分",_qh_orderModel.qh_driverScore];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = @"评价";
    [self.qh_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
}

#pragma mark --Delegate

#pragma mark --TargetMethod
- (void)qh_clickedRightButton:(UIButton *)sender {
    DLog(@"评价");
    
    NSString *APIURLString = [[NSString qh_APIURLString] stringByAppendingPathComponent:@"/buAssessment/1/add"];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setObject:_qh_orderModel.qh_orderID forKey:@"waybillId"];
    
    if ([USER_GET_TYPE isEqualToString:USER_TYPE_D]) {
        
        [parameters setObject:_qh_orderModel.qh_driverID forKey:@"evaluationPeople"];
        [parameters setObject:_qh_orderModel.qh_ownerID forKey:@"byEvaluation"];
        [parameters setObject:@"2" forKey:@"state"];
    } else {
        [parameters setObject:_qh_orderModel.qh_driverID forKey:@"byEvaluation"];
        [parameters setObject:_qh_orderModel.qh_ownerID forKey:@"evaluationPeople"];
        [parameters setObject:@"1" forKey:@"state"];
    }
    if (rootView.qh_scoreView.ratingValue != 0) {
        
        NSNumber *ratingValue = [NSNumber numberWithInteger:rootView.qh_scoreView.ratingValue];
        [parameters setObject:ratingValue forKey:@"score"];
    }
    if (rootView.qh_textView.text) {
        
        [parameters setObject:rootView.qh_textView.text forKey:@"evaluation"];
    } else {
        
        [parameters setObject:@"该用户未填写评价内容" forKey:@"evaluation"];
    }
    
    [GQHNetHelper qh_requestGET:APIURLString parameters:parameters success:^(NSNumber *code, NSString *message, NSDictionary *data) {
        DLog(@"%@---%@---%@",code,message,data);
        
        if (0 == [code integerValue]) {
            
            [self showToastWithText:message];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            
            [self showToastWithText:message];
        }
    } failure:^(NSError *error) {
        
        NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
        [self showToastWithText:errorString];
    }];
}
#pragma mark --PrivateMethod

#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
